﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ample.Model;

namespace Ample.BLL
{
    [System.ComponentModel.DataObject]
    public class ProductBO
    {        
        #region Get Methods

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
        public IEnumerable<Product> GetProducts()
        {
            return DAL.ProductManager.Create().GetProducts();
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public Product GetProductById(int prodId)
        {
            return DAL.ProductManager.Create().GetProductById(prodId);
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public IEnumerable<Product> GetProductByTypeId(int prodTypeId)
        {
            if (prodTypeId < 0)
                return GetProducts();
            else
                return DAL.ProductManager.Create().GetProductByTypeId(prodTypeId);
        }
        
        #endregion


        #region Insert Methods

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int InsertProduct(Product prod)
        {
            //ApplyRules(prod);
            OperationStatus opStatus = DAL.ProductManager.Create().InsertProduct(prod);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.ProductRules.InsertProduct: {0}", opStatus.Exception.Message));
            }

            return opStatus.InsertedRowID;
        }

        #endregion


        #region Update Methods

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update, true)]
        public void UpdateProduct(Product prod)
        {
            //ApplyRules(prod);
            OperationStatus opStatus = DAL.ProductManager.Create().UpdateProduct(prod);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.ProductRules.UpdateProduct: {0}", opStatus.Exception.Message));
            }
        }
        
        #endregion


        #region Delete Methods

        public static void DeleteProduct(Product prod)
        {
            OperationStatus opStatus = DAL.ProductManager.Create().DeleteProduct(prod);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.ProductRules.DeleteProduct: {0}", opStatus.Exception.Message));
            }
        }

        #endregion

        #region Business Rules

        private static void ApplyRules(Product prod)
        {

        }

        #endregion

    }
}
